import axios from 'axios';
import { objectToParams } from '@/helpers/AxiosHelper';
import { PaginateConfig, PaginateResult } from '@/type/Shared';
import { User } from '@/type/Auth';
import { getAuth } from '.';

async function getUsers(config: PaginateConfig = {}): Promise<PaginateResult<User[]>> {
    return axios.get(`/users?${objectToParams(config)}`, getAuth());
}

async function getUser(id: number): Promise<User> {
    return axios.get(`/users/${id}`, getAuth());
}

async function createUser(user: User, roles: number[]): Promise<User> {
    return axios.post('/users', { user, roles }, getAuth());
}

async function updateUser(user: User, roles: number[]): Promise<User> {
    return axios.put(`/users/${user.id}`, { user, roles }, getAuth());
}

async function deleteUser(id: number): Promise<boolean> {
    return axios.delete(`/users/${id}`, getAuth());
}

async function logoutAllUser(): Promise<boolean> {
    return axios.post('/users/logout-all', {}, getAuth());
}

async function logoutUser(id: number): Promise<boolean> {
    return axios.post(`/users/${id}/logout`, {}, getAuth());
}

async function generateLoginOtp(): Promise<string> {
    return axios.post('/user-otps', {}, getAuth());
}

async function clearLoginOtp(): Promise<string> {
    return axios.delete('/user-otps', getAuth());
}

export default {
    getUsers,
    getUser,
    createUser,
    updateUser,
    deleteUser,
    logoutAllUser,
    logoutUser,
    generateLoginOtp,
    clearLoginOtp,
};
